import jsonwebtoken from 'jsonwebtoken'

import StorageUtil from "./StorageUtil";
// import * as fs from 'fs';
// var fs = require("fs");

// const cert = fs.readFileSync('../../../pubkey.pem');
const cert = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfJfdbHYckEYKdmUTj9E6gQ2YT
JrH8RoZ9N2h2zJz2FmrteW1laMdUKIcv8FSmvMLjNDpCDFV45Z3h8b4ARENJDZQl
81Fn6JVQmrrXzqdN6VOC07KB88hd9nZHgHK4bu3bYkjt8JVk8QQKUHpl/59TPyEh
DC8ot78hjKeqNypyBQIDAQAB
-----END PUBLIC KEY-----
`;


console.log(cert);

function decode(jwt, fok, ferr) {
    jsonwebtoken.verify(jwt, cert, function(err, decoded){
        if (err) {
            ferr(err);
        } else if (decoded) {
            StorageUtil.setJwt(jwt, decoded);
            fok(decoded);
        } 
    });
}

export default { decode }
